<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BlogController
 *
 * @author koifl_000
 */
class BlogController extends FrontEndController {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     *
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', 'actions' => array('create'),
                'roles' => array('vip')
            ),
            array('allow', 'actions' => array('comment','like'),
                'roles' => array('member')
            ),
            array('allow', 'actions' => array('view'),
                'users' => array('*')
            ),
            array('deny', 'users' => array('*')
            )
        );
    }

    public function actionView($id) {
        $model = $this->loadBlog($id);
        if (!empty($_SERVER ['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER ['HTTP_CLIENT_IP'];
            // Is it a proxy address
        } elseif (!empty($_SERVER ['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER ['REMOTE_ADDR'];
        }
        if (Yii::app()->user->isGuest) {
            $check = BlogView::model()->find('blog_id=:id and ip =:ip and view_at >DATE_SUB(NOW(), INTERVAL 1 DAY)', array(
                ':id' => $id,
                ':ip' => $ip
            ));
            if (!$check) {
                $blogView = new BlogView ();
                $blogView->blog_id = $model->id;
                $blogView->view_at = date("Y-m-d H:i:s");
                $blogView->ip = $ip;
                $blogView->insert();
                $model->view_cnt = $model->view_cnt + 1;
                $model->update();
            }
        } else {
            $check = BlogView::model()->find('blog_id=:id and user_id =:user_id and view_at >DATE_SUB(NOW(), INTERVAL 1 DAY)', array(
                ':id' => $id,
                ':user_id' => Yii::app()->user->id
            ));
            if (!$check) {
                $blogView = new BlogView ();
                $blogView->blog_id = $model->id;
                $blogView->view_at = date("Y-m-d H:i:s");
                $blogView->user_id = Yii::app()->user->id;
                $blogView->insert();
                $model->view_cnt = $model->view_cnt + 1;
                $model->update();
            }
        }
        $comment = new CommentBlog ();
        $allComment = CommentBlog::model()->findAll('blog_id=:blog_id and status = :status', array(':blog_id' => $id, ':status' => CommentBlog::COMMENT_ACTIVE));
        $this->render('view', array('model' => $model,'comment'=>$comment,'allComment'=>$allComment));
    }
    
    public function actionLike($id) {
        $result = array();
        $blog = $this->loadBlog($id);
        $check = BlogLike::model()->find("user_id = :user_id and blog_id =:blog_id", array(
            ':blog_id' => $id,
            'user_id' => Yii::app()->user->id
        ));
        if ($check) {
            $check->delete();
            $blog->like_cnt = $blog->like_cnt - 1;
            $blog->update();
            $result ['meg'] = 'Thích';
        } else {
            $model = new BlogLike ();
            $model->blog_id = $id;
            $model->user_id = Yii::app()->user->id;
            $model->insert();
//            if ($model->insert()) {
//                $userActive = new UserActive ();
//                $userActive->user_id = Yii::app()->user->id;
//                $userActive->image_id = $id;
//                $userActive->active_at = date('Y-m-d H:i:s');
//                $userActive->active_type = UserActive::LIKE_IMG;
//                $userActive->insert();
//            }
            $blog->like_cnt = $blog->like_cnt + 1;
            $blog->update();
            $result ['meg'] = 'Bỏ thích';
        }

        $result ['like_cnt'] = $blog->like_cnt;

        echo json_encode($result);
    }
    
    public function actionComment($id) {
        $comment = new CommentBlog ();
        if (isset($_POST ['CommentBlog'])) {
            $comment->attributes = $_POST ['CommentBlog'];
            $comment->comment_at = date("Y-m-d H:i:s");
            $comment->user_id = Yii::app()->user->id;
            $comment->blog_id = $_GET ['id'];
            $comment->content = CHtml::encode($comment->content);
            if ($comment->save()) {                
//                $userActive = new UserActive ();
//                $userActive->active_type = UserActive::COMMENT_IMG;
//                $userActive->active_at = date('Y-m-d H:i:s');
//                $userActive->image_id = $_GET ['id'];
//                $userActive->user_id = Yii::app()->user->id;
//                $userActive->save();

                $this->redirect(array(
                    'view',
                    'id' => $id
                ));
            }
        }
        $this->redirect(array(
            'view',
            'id' => $id
        ));
    }

    public function actionCreate() {
        $blog = new Blog();
        if (isset($_POST['Blog'])) {
            $blog->attributes = $_POST['Blog'];
            $blog->created_at = date("Y-m-d H:i:s");
            $blog->user_created = Yii::app()->user->id;
            if ($blog->save()) {
                $this->redirect(array('view', 'id' => $blog->id));
            }
        }
        $this->render('create', array('model' => $blog));
    }

    public function loadBlog($id) {
        $model = Blog::model()->findByPk($id, 'status=1');
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}

?>
